package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Home.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import gs.*;
	import gs.easing.*;
	import gs.events.TweenEvent;
	
	//import mx.containers.Panel;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.plaf.*;
	import org.aswing.skinbuilder.SkinProgressBarBackground;
	import org.aswing.skinbuilder.SkinProgressBarForeground;
	
	public class PanelMainAcceptedTaskItem extends JPanel
	{
		//members define
		private var panelType:JPanel;
		private var textName:JTextArea;
		private var testGoal0:JTextArea;
		private var testGoal1:JTextArea;
		private var testGoal2:JTextArea;
		private var testGoal3:JTextArea;
		private var testGoal4:JTextArea;
		public function PanelMainAcceptedTaskItem()
		{
			//component creation
			setSize(new IntDimension(190, 110));
			setPreferredSize(new IntDimension(190, 110));
			var layout0:EmptyLayout = new EmptyLayout();
			setLayout(layout0);
			
			panelType = new JPanel();
			panelType.setLocation(new IntPoint(0, 0));
			panelType.setSize(new IntDimension(20, 20));
			panelType.setPreferredSize(new IntDimension(20, 20));
			
			textName = new JTextArea();
			textName.setLocation(new IntPoint(20, 0));
			textName.setSize(new IntDimension(170, 20));
			textName.setPreferredSize(new IntDimension(170, 20));
			
			testGoal0 = new JTextArea();
			testGoal0.setLocation(new IntPoint(0, 20));
			testGoal0.setSize(new IntDimension(190, 18));
			testGoal0.setPreferredSize(new IntDimension(190, 18));
			
			testGoal1 = new JTextArea();
			testGoal1.setLocation(new IntPoint(0, 38));
			testGoal1.setSize(new IntDimension(190, 18));
			testGoal1.setPreferredSize(new IntDimension(190, 18));
			
			testGoal2 = new JTextArea();
			testGoal2.setLocation(new IntPoint(0, 56));
			testGoal2.setSize(new IntDimension(190, 18));
			testGoal2.setPreferredSize(new IntDimension(190, 18));
			
			testGoal3 = new JTextArea();
			testGoal3.setLocation(new IntPoint(0, 74));
			testGoal3.setSize(new IntDimension(190, 18));
			testGoal3.setPreferredSize(new IntDimension(190, 18));
			
			testGoal4 = new JTextArea();
			testGoal4.setLocation(new IntPoint(0, 92));
			testGoal4.setSize(new IntDimension(190, 18));
			testGoal4.setPreferredSize(new IntDimension(190, 18));
			
			//component layoution
			append(panelType);
			append(textName);
			//append(testGoal0);
			//append(testGoal1);
			//append(testGoal2);
			//append(testGoal3);
			//append(testGoal4);
			
			arrayTaskGoals.push(testGoal0);
			arrayTaskGoals.push(testGoal1);
			arrayTaskGoals.push(testGoal2);
			arrayTaskGoals.push(testGoal3);
			arrayTaskGoals.push(testGoal4);
			
			testGoal0.addEventListener(MouseEvent.CLICK,onClickGoal0);
			testGoal1.addEventListener(MouseEvent.CLICK,onClickGoal1);
			testGoal2.addEventListener(MouseEvent.CLICK,onClickGoal2);
			testGoal3.addEventListener(MouseEvent.CLICK,onClickGoal3);
			testGoal4.addEventListener(MouseEvent.CLICK,onClickGoal4);
			
			var filter:GlowFilter;
			filter = new GlowFilter(0x000000, 1, 2, 2, 100);
			textName.filters = (new Array(filter));
			
			textName.setBackgroundDecorator(null);
			textName.setEditable(false);
			textName.setForeground(ASColor.WHITE);
			textName.setBorder(new EmptyBorder);
			//textName.setBounds(new IntRectangle(0,0,0,0));
			textName.mouseEnabled = false;
			for each(var area:JTextArea in arrayTaskGoals)
			{
				area.setBackgroundDecorator(null);
				area.setEditable(false);
				area.setForeground(ASColor.GREEN);
				area.setBorder(new EmptyBorder);
				area.filters = (new Array(filter));
				//area.setBounds(new IntRectangle(0,0,0,0));
				area.mouseEnabled = true;
			}
			//this.setPreferredWidth(190);
			
		}
		private function onClickGoal0(e:Event):void
		{
			onClickGoal(_item.task_goals[0]);
		}
		private function onClickGoal1(e:Event):void
		{
			onClickGoal(_item.task_goals[1]);
		}
		private function onClickGoal2(e:Event):void
		{
			onClickGoal(_item.task_goals[2]);
		}
		private function onClickGoal3(e:Event):void
		{
			onClickGoal(_item.task_goals[3]);
		}
		private function onClickGoal4(e:Event):void
		{
			onClickGoal(_item.task_goals[4]);
		}
		private function onClickGoal(objGoal:Object):void
		{
			//objGoal.goal_map_id = msg[nIndex++];
			//objGoal.goal_npc_id = msg[nIndex++];
			//objGoal.goal_position_map_id = msg[nIndex++];
			//objGoal.goal_position_map_x = msg[nIndex++];
			//objGoal.goal_position_map_y = msg[nIndex++];
			
			//FrameInfo.showMessageDialog("","你已经到了"+linkEvent.text+"!");
			//var dtemp:Array = (linkEvent.text as String).split(',');
			//if(dtemp.length == 3)
			//{
			//	Client.getInstance().sendPacketLogic(9,[int(dtemp[0]),CharInfo.mapLineId,int(0),int(0),int(dtemp[1]),int(dtemp[2])]);
			//}
			if(objGoal.goal_npc_id != 0)
			{
				PanelMainHelp.tmpMapId = int(objGoal.goal_map_id);
				PanelMainHelp.tmpNpcId = int(objGoal.goal_npc_id);
				var objMapCSV:Object = Client.getInstance().GetMapCSV(PanelMainHelp.tmpMapId);
				if(objMapCSV == null)
				{
					return;
				}
				if(PanelMainHelp.tmpMapId != CharInfo.mapId)
				{
					PanelMainHelp.tmpMapX = int(objMapCSV.base_x);
					PanelMainHelp.tmpMapY = int(objMapCSV.base_y);
					Client.getInstance().sendPacketLogic(9,[int(PanelMainHelp.tmpMapId),CharInfo.mapLineId,int(0),int(0),int(objMapCSV.base_x),int(objMapCSV.base_y)]);
				}
				else
				{
					var npc:ObjectCharNPC = SceneMain.getInstance().getObjectByNpcId(PanelMainHelp.tmpNpcId);
					if(npc != null)
					{
						if(Point.distance(npc.getPos(),CharInfo.objSelf.getPos()) > 100)
						{
							PanelMainHelp.tmpMapX = int(npc.getPos().x);
							PanelMainHelp.tmpMapY = int(npc.getPos().y);
							Client.getInstance().sendPacketLogic(9,[int(PanelMainHelp.tmpMapId),CharInfo.mapLineId,int(0),int(0),int(npc.getPos().x),int(npc.getPos().y)]);
							return;
						}
						
						FrameNPCDialog._currentCharNpc = npc;
						if(FrameNPCDialog._currentCharNpc != null)
						{
							Client.getInstance().sendPacketLogic(900,[int(PanelMainHelp.tmpNpcId)]);
							PanelMainHelp.tmpNpcId = -1;
							PanelMainHelp.tmpMapId = -1;
							PanelMainHelp.tmpMapX = 0;
							PanelMainHelp.tmpMapY = 0;
						}
					}
				}
			}
			else
			{
				Client.getInstance().sendPacketLogic(9,[int(objGoal.goal_position_map_id),CharInfo.mapLineId,int(0),int(0),int(objGoal.goal_position_map_x),int(objGoal.goal_position_map_y)]);
			}
		}
		private var arrayTaskGoals:Array = [];
		static public function formatTime(time:int):String
		{
			var str:String = new String;
			var hour:int = time / 3600;
			var min:int = (time % 3600)/60;
			var second:int = time%60;
			if(hour > 0)str += ((hour < 10) ? "0" : "") + hour+':';
			if(min >= 0)str += ((min < 10) ? "0" : "") + min+':';
			str += ((second < 10) ? "0" : "") + second;
			return str;
		}
		
		private var _item:Object = null;
		
		public function set item(val:Object):void
		{
			_item = val;
			
			for(var i:int = 0; i < 5; i++)
			{
				if(contains(arrayTaskGoals[i]))
					remove(arrayTaskGoals[i]);
			}
			
			if(_item == null)
			{
				return;
			}
			
			var text:String = '';
			var objCSVInfo:Object = Client.getInstance().GetTaskCSV(_item.task_id);
			if(objCSVInfo == null)
				return;
			text += '<font color="#FFFF00">';
			text += '['+objCSVInfo.require_level+']'
			text += _item.task_name+'</font>';
			if(_item.task_step == _item.task_step_size)
				text +='<font color="#00FF00">(完成)</font>';
			else
				text += '('+_item.task_step+'/'+int(_item.task_step_size)+')';
			if(objCSVInfo.enable_record == 1)
			{
				text +='<font color="#00FF00">\t'+ formatTime(_item.task_elapsed_time)+'</font>'
			}
			
			textName.setHtmlText(text);
			textName.pack();
			textName.paintImmediately();
			text ='';
			//text += _objTaskInfo.task_goal+'<br>';
			i = 0;
			for each(var objGoal:Object in _item.task_goals)
			{
				if((objGoal.goal_desc as String).length == 0)
					break;
				text ='';
				text += '    <a href=\"event:0\">'+objGoal.goal_desc;
				if(objGoal.goal_count_size > 0)
					text +='('+objGoal.goal_count+'/'+objGoal.goal_count_size+')';
				text +='</a>';
				arrayTaskGoals[i].setHtmlText(text);
				if(!this.contains(arrayTaskGoals[i]))
					append(arrayTaskGoals[i]);
				arrayTaskGoals[i].setLocation(new IntPoint(0, 20 + 18*i));
				arrayTaskGoals[i].pack();
				arrayTaskGoals[i].paintImmediately();
				i++;
			}
			
			setPreferredSize(new IntDimension(190, 20+18*i));
			if(objCSVInfo.type == 0)panelType.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_task_type_0") as DisplayObject));
			if(objCSVInfo.type == 1)panelType.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_task_type_1") as DisplayObject));
			if(objCSVInfo.type == 2)panelType.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_task_type_2") as DisplayObject));
			if(objCSVInfo.type == 3)panelType.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_task_type_3") as DisplayObject));
			if(objCSVInfo.type == 4)panelType.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_task_type_4") as DisplayObject));
			
			pack();
			this.paintImmediately();
		}
		public function get item():Object
		{
			return _item;
		}
		
	}
}